/*function A(x){
　　this.x=x;
     function b(){
          alert("1")
     }
}
A.prototype.say=function(){
     alert("22")
};
var obj=new A(1);
var num=0;
for(i in A.prototype){
    // alert(i);
     num++;
}
obj.say();*/
//alert(A.prototype.say)*/



/*var add=(function () {
    var counter = 0;
    return function () {return counter += 1;}
})();


function myFunction(){
     alert("fff")
     function inner(){
          //alert("1")
          document.getElementById("demo").innerHTML = add();   
     }
     return function() {alert("d")};
}


function newLoad(){
     for(var i = 0;i < 3; i++){
          var anchor = document.getElementById("anchor"+i);
          f2(anchor,i);
     }
     
}

function f2(anchor,i){
     anchor.onclick = function(){
          alert(i);
     }
}

window.onload = newLoad;*/



/*function Foo(y){ 
this.y = y ; 
} 

Foo.x = 10; 

Foo.calculate = function(z){ 
return this.x+this.y+z; 
}; 

var b = new Foo(20); 

alert(b.calculate(30));*/

/*for(var  i=0;i<4;i++){
 
   alert("内"+i) 
}
alert("外"+i)*/

/*var Calculaotr = function(x, y) {
    this.x = x;
    this.y = y;
};
Calculaotr.prototype = function() {
    add= function (x,y) {
        return x + y;
    },
    subtract=function (x,y) {
        return x - y;
    }

    return {
        A:add,
        S:subtract
    } 
}();

var o = Calculaotr.prototype;
o.A(3,2)
console.log(o.A(3,2));
*/



/*String.prototype.distinct = function(){
  var a = this.split("");
  var b = [];
  for(var i=0;i<a.length;i++){
    if(a.indexOf(a[i])!=i){
      b.push(a[i]);
    }
  }
  return b.join("");
}
"abcabcdaaa".distinct();
*/
/*var s = "abcabcdaaa";
ss = s.split("");// 在每个逗号(,)处进行分解。*/


/*Array.prototype.delrepeat = function() {
  var arr = this;
  var _arr = new Array();
  for (var i in arr) {
    if (i == 'delrepeat') continue;
    if (_arr.length == 0) _arr.push(arr[i]);
    for (var j = 0; j < _arr.length; j++) {
      if (arr[i] == _arr[j]) {
        break;
      }
      if (j > _arr.length - 2) _arr.push(arr[i]);
    }
  }
  return _arr;
}
alert([1, 1, 1, 1, 2, 2, 3, 4, 5, 5, 5].delrepeat());*/
//alert(['a','b','c','d','b','a','e'].distinct());


/*function mySort() {
    var tags = new Array();
    for(var i = 0;i < arguments.length;i++) {
        tags.push(arguments[i]);
    }
    tags.sort(function(compare1,compare2) {
        return compare1- compare2;
    });

    tags = arguments;
    for(var i=0,l = tags.length;i<l;i++){
      for(var j =i+1;j<l;j++){
        if(tags[i]>tags[j]){
          var temp = tags[i];
          tags[i] = tags[j];
          tags[j] = temp;
        }
      }
    }

    return tags;
}*/

/*
function quickSort(array) {
  function sort(prev, numsize) {
    var nonius = prev;
    var j = numsize - 1;
    var flag = array[prev];
    if ((numsize - prev) > 1) {
      while (nonius < j) {
        for (; nonius < j; j--) {
          if (array[j] < flag) {
            array[nonius++] = array[j];　 //a[i] = a[j]; i += 1;
            break;
          };
        }
        for (; nonius < j; nonius++) {
          if (array[nonius] > flag) {
            array[j--] = array[nonius];
            break;
          }
        }
      }
      array[nonius] = flag;
      sort(0, nonius);
      sort(nonius + 1, numsize);
    }
  }
  sort(0, array.length);
  return array;
}
var a =[50, 32, 11, 16, 32, 24, 99, 57, 100]
var result = quickSort(a);
console.info(result);*/

/*function sort(a){  
  for(var i=0;i<a.length-1;i++){
    for(var j=i+1;j<a.length-1;j++){
      if(a[i]>a[j]){
        var temp = a[i];
        a[i]=a[j];
        a[j]=temp;
      }
    }
  }
  return a;
}

var a = [];
sort(a)*/


/*var Obj = function(msg){
    this.msg = msg;
    this.shout = function(){
        alert(this.msg);
    }  
    this.waitAndShout = function(){
      setTimeout(this.shout(),5000);
        //隔五秒钟后执行上面的shout方法
    }
}*/

/*String.prototype.trim = function(){
  var str;
  var b = new Array;
  a = this.split("");
  for(var i= 0;i<a.length;i++){
    if(a[i]!=" "){
      b.push(a[i]);
    }
  }
  str = b.join("");
  return str;
}
console.log("    fff  d w ".trim());*/


/*function parseQueryString(url){
      var obj = {};
      var a1 = url.split('?');
      obj.target = a1[0];
      var a2 = a1[1].split('&');
      for(var i =0;i<a2.length;i++){
        var a3 = a2[i].split('=');
        obj[a3[0]]=a3[1];
      }
      return obj;
    }
*/

/*function test(){
          this.name = 'taobao';
          var waitMes = function (){
          //每隔5秒钟执行this.name
          setTimeout(function (){alert(self.name)},5000);
           }
         return waitMes;
}
var _test = test();
_test();
*/
/*
function getUrl(url){
  //var arr=url.split('?')[1].split('&'); 
  var arr = [];
  var a3= [];
  arr = url.split("?");
  a1 = arr[1];
  a2 = a1.split("&");
  obj = {};
  for(var i=0;i<a2.length;i++){
    a3[i] = a2[i].split("=");
    obj[a3[i][0]] = a3[i][1];
  }
return obj;

}
var url= "URL:http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx&e";
//console.log(getUrl(url));
*/


/*var getjson = function(url) {
  var a = [];
  var obj = {};
  var a = url.split('?')[1].split('&');
  for (var i = 0; i < a.length; i++) {
    var b[i] = a[i].split('=');
    obj[b[i][0]] = b[i][1];
  }
  return obj;
}

getjson(url);*/


/*var str = "abcdefgaddda";　　
var obj = {};　　
for (var i = 0, l = str.length; i < l; i++) {　　
  var key = str[i];　　
  if (!obj[key]) {　　
    obj[key] = 1;　　
  } else {　　
    obj[key]++;　　
  }　　
}　　

var max = -1;　　
var max_key = "";　　
var key;　　
for (key in obj) {　　
  if (max < obj[key]) {　　
    max = obj[key];　　
    max_key = key;　　
  }　　
}　　
alert("max:" + max + " max_key:" + max_key);*/

/*var cloneObj = function(obj){
    var str, newobj = obj.constructor === Array ? [] : {};
    if(typeof obj !== 'object'){
        return;
    } else if(window.JSON){
        str = JSON.stringify(obj), //系列化对象
        newobj = JSON.parse(str); //还原
    } else {
        for(var i in obj){
            newobj[i] = typeof obj[i] === 'object' ? 
            cloneObj(obj[i]) : obj[i]; 
        }
    }
    return newobj;
};


//测试
var obj = {a: 0, b: 1, c: 2};
var arr = [0, 1, 2];
//执行深度克隆
var newobj = cloneObj(obj);
var newarr = cloneObj(arr);

//对克隆后的新对象进行成员删除
delete newobj.a;
newarr.splice(0,1);
console.log(obj, arr, newobj, newarr);
//结果： {a: 0, b: 1, c: 2}, [0, 1, 2], {b: 1, c: 2}, [1, 2];

*/


/*function quicksort(n,left,right){
  var p;
  if(left<right){
    p = position(n,left,right);
    quicksort(n,left,p-1);
    quicksort(n,p+1,right);
  }
}

function position(n,left,right){
  var temp = n[left];
  while(left<right){
    while(left<right&&n[right]>temp)
      right--;
    if(left<right)
      n[left++]=n[right];
    while(left<right&&n[left]<temp)
      left++;
    if(left<right)
      n[right--]=n[left];
  }
  n[left]=temp;
  return left;
}

var a =[50, 32, 11, 16, 32, 24, 99, 57, 100];
quicksort(a,0,a.length-1)
console.log(a);*/

/*function fibonacci(){
  var a =[];
  a[0] = 1; a[1] =1;
  for(var i=2;i<100;i++){
    a[i]= a[i-1]+a[i-2];
  }
  return a;
   
}

function getNthFibonacci(count){
  var a = fib();
  alert(a[count]);
}

getf(4);*/

/*function obj(name){
 if (name) { this.name = name; } return this;
}

obj.prototype.name = "name2";
var a = obj("name1");
var b = new obj;

console.log(a.name + b.name);*/


/*var clock={
  hour:12,
  second:10,
  show:function(){
    console.log(this.hour)
  }
}
clock.show()
*/
/*var lev = function() {
  return "脚本之家";
};

function Parent() {
  this.name = "脚本";
  this.age = "30";
  this.lev = lev;
};
var x = new Parent();
alert(x.name);
alert(x.lev());*/

/*function aa(){
  var a =[];
  var b=[];
  for(var i=1;i<50;i++){
    a.push(i);
  }
  for(var i=0;i<a.length;i++){
    for(var j=0;i<a.length;j++){
      if(a[i]*2==a[j]){
        b.push(a[i]);
      }
    }
    
  }
  return b;
}
console.log(aa());*/

/*function clone(obj){
  var buf;
  if(obj instanceof Array){
    buf = [];
    var i = obj.length;
    while(i--){
      buf[i] = clone(obj[i]);
    }
    return buf;
  }else if(obj instanceof Object){
    buf = {};
    for(var k in obj){
      buf[k] = clone(obj[k]);
    }
    return buf;
  }else{
    return obj;
  }
}

var a = [1,2,3,4];
clone(a)
*/
//深度克隆
/*  function clone(Obj) {   
        var buf;   
        if (Obj instanceof Array) {   
            buf = [];  //创建一个空的数组 
            var i = Obj.length;   
            while (i--) {   
                buf[i] = clone(Obj[i]);   
            }   
            return buf;   
        }else if (Obj instanceof Object){   
            buf = {};  //创建一个空对象 
            for (var k in Obj) {  //为这个对象添加新的属性 
                buf[k] = clone(Obj[k]);   
            }   
            return buf;   
        }else{   
            return Obj;   
        }   
    } 

    
    function clone2(obj){
      var temp;
      if(obj instanceof Array){
        temp = [];
        for(var i in obj){
          temp[i] = obj[i];
        }
        return temp;
      }else if(obj instanceof Object){
        temp = {};
        for(var i in obj){
          temp[i] = obj[i];
        }
        return temp;
      }

    }


    function clone1(obj){
      var c = [];
      for(var i in obj){
        c[i] = obj[i];
      }
      return c;
    }

    var a =[1,2,3,4];
    var b = clone1(a);
    b.splice(0,2);
    console.log(a);
    console.log(b);*/

/*   function createPerson(name,age){
      var Person = new Object();
      Person.name = name;
      Person.age = age;
      Person.say = function say(){
        alert("aaa")
      }
      return Person;
   }

   var p1 = new createPerson("summer",12);
   var p2 = new createPerson("json",22);
   console.log(p1.say==p2.say);
   p1.say();
   p2.say();*/

/*   function Person(name,age){
    this.name=name;
    this.age=age;
    this.say=function say(){
      alert("sss")
    }
   }

   var p1 = new Person("summer",12)
   p1.say();*/

/*   function Person(){

   }

   Person.prototype.name="summer";
   Person.prototype.age=12;
   Person.prototype.say=function(){
    alert(this.name)
   }

   var p1 = new Person();
   var p2 = new Person();
   console.log(p1.say==p2.say);
   p1.name="josn";
   p1.say();*/

/*   function Person(name,age){
    this.name=name;
    this.age=age;
   }

   Person.prototype.say=function say(){
    alert(this.name);
   }

   var p1= new Person("sum",11);
   var p2 = new Person("dd",44);
   console.log(p1.say == p2.say);
   p1.say();
   p2.say();*/

/*function Person(){
    var name;
    var age;
    setAge=function(age){
        this.age = age;
    }

    
  return {
    setA:setAge,
  }
}

var p1 = Person();
var p2 = Person();
p1.setA(12);
alert(p1.age)

console.log(p1.setA == p2.setA)*/


/*var person = function(){    
    //变量作用域为函数内部，外部无法访问    
    var name = "dddd";  
    var age = 12;     
       
    return {    
       getName : function(){    
           return name;    
       },    
       setName : function(newName){    
           name = newName;    
       }    
    }    
}; 

var p1= new person();
var p2=new person();
p1.setName("ssss");
console.log(p1.getName())*/



/*(function(){
  var parent = function(age){
    return new parent.prototype.init();
  };
  parent.prototype ={
    init:function(){
      return this;
    },
    say:function(){
      alert(this.age)
    },
    age:20,
  }

  parent.prototype.init.prototype=parent.prototype;

  window.parent = window.$=parent;
})()

parent();
*/

/*var $$ = parent = function(age){
  return new parent.fn.init(age);
}

parent.fn = parent.prototype = {
  name:"summer",
  init:function(age){
    this.age=age;
    return this;
  },
  constructor:parent
}
parent.fn.init.prototype=parent.fn;

parent.fn.setAge = function(newAge){
  this.newAge = newAge;
  return this;
}*/

/*function Dog(name, color) {
  this.name = name || "";
  this.color = color || "";
}
Dog.prototype.setName = function(name) {
  this.name = name;
  return this;
};
Dog.prototype.setColor = function(color) {
  this.color = color;
  //return this;
};
Dog.prototype.yelp = function() {
  alert("我的名字叫：" + this.name + ",我的颜色是:" + this.color);
  //return this;
};


var dog = new Dog();
dog.setName("summer").setColor(12)*/


/*function Myevent(){
  this.handler;
}

Myevent.prototype={
  addHandler:function(handler){
    this.handler=handler;
  },
  fire:function(){
    this.handler();
  },
  removeHandler:function(){
    this.handler=null;
  }
}

function init(){
  var mye = new Myevent();
  mye.addHandler(myMethod);
  mye.fire();

}

function myMethod(){
  alert("dd")
}

//init();

function f2(){
  alert("f2")
}

function f1(){
  setTimeout(function(){
    var mye = new Myevent();
    mye.addHandler(myMethod);
    mye.fire();
    alert("f1");
  },1000)
}
*/

var m  ={
  x : 1
}

function a(y){
  console.log(this.x+y)
}

a.apply(m,[5]);

/*var foo = a.bind(m,666);
foo();*/

Function.prototype.mybind = function(o,args) {
   var self=this, boundArgs=arguments;
        return function(){
            var args=[],i;
            for(i=1; i<boundArgs.length; i++) args.push(boundArgs[i]);
            for(i=0; i<arguments.length; i++) args.push(arguments[i]);
         
            return self.apply(o, args);
        };
};

Function.prototype.mybind2 = function(o,args) {
    var self=this;
    return function(){
        return self.call(o, args);
    };
};
//a.mybind(m,666)();
/*var foo = a.mybind2(m,666);
foo();*/
this.x = 9; 
var module = {
  x: 81,
  getX: function() { return this.x; }
};

module.getX(); // 81

var retrieveX = module.getX;
retrieveX(); // 9, because in this case, "this" refers to the global object

// Create a new function with 'this' bound to module
//New programmers (like myself) might confuse the global var getX with module's property getX
var boundGetX = retrieveX.bind(module);
boundGetX(); // 81
retrieveX.call(module);
